const DB = require('../core/DB')
const {
    Op
} = require('sequelize')

const Mail = require('../core/Mail')

class SystemController {
    // 获取用户ip
    async getIP(ctx) {
        global.Response.success(ctx, undefined, {
            ip: global.utils.getClientIP(ctx)
        })
    }

    // 统计IP
    async countIP(ctx) {
        let { ip, page = 1, size = 10 } = ctx.query
        page = Number(page)
        size = Number(size)
        const {count,rows} = await DB.ip.findAndCountAll({
            where:{
                ip
            },
            order: [
                ['id', 'DESC']
            ],
        })
        const data = {
            rows,
            meta: {
                page: page, // 当前页数
                size, // 每页条目
                count: count, // 条目总数
                total: Math.ceil(count / size), // 总页数
            }
        }
        global.Response.success(ctx, undefined, data)
    }


    // 获取用户ip记录
    async getIPLog(ctx) {
        // 参数
        let { page = 1, size = 10 } = ctx.query

        page = Number(page)
        size = Number(size)

        const {
            count,
            rows
        } = await DB.ip.findAndCountAll({
            offset: (page - 1) * size,
            limit: size,
            order: [
                ['id', 'DESC']
            ],
        })
        const data = {
            rows,
            meta: {
                page: page, // 当前页数
                size, // 每页条目
                count: count, // 条目总数
                total: Math.ceil(count / size), // 总页数
            }
        }
        global.Response.success(ctx, undefined, data)
    }

}

module.exports = new SystemController